package cn.yjr.vector;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CookieStore;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.cookie.Cookie;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import cn.yjr.vector.tools.MD5;
import cn.yjr.vector.tools.SharedPreferencesHelper;
import cn.yjr.vector.tools.YJRHttpClient;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockActivity;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.PersistentCookieStore;
import com.loopj.android.http.RequestParams;

public class AddExperienceActivity extends SherlockActivity implements OnClickListener{

	LayoutInflater inflater;
	ActionBar actionBar;
	View title_bar;
	Context context;
	String response;
	JSONObject jsonInfo;
	SharedPreferencesHelper spHelper;
	RelativeLayout begin_time_rl, end_time_rl, title_rl, hospital_rl, department_rl;
	TextView save_tv, begin_time_tv, end_time_tv, title_tv,hospital_tv, department_tv;
	//声明一个独一无二的标识，来作为要显示DatePicker的Dialog的ID：  
    static final int DATE_DIALOG_ID = 0; 
    static final int DATE_DIALOG_ID_END = 1;
    private int bYear = 0;  
    private int bMonth = 0;
    private int eYear = 0;  
    private int eMonth = 0;
    String title = null, hospital = null, department = null, title_name = null;
    
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_add_experience);
		inflater = this.getLayoutInflater();
		actionBar = getSupportActionBar();
		context = this;
		this.spHelper = new SharedPreferencesHelper(this.context);
		initTitleBar();
		
		initComponent();
		
		initEventListener();
		
		
		
	
	}
	
	private void initTitleBar(){
		ActionBar.LayoutParams lp = new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT,ActionBar.LayoutParams.WRAP_CONTENT,Gravity.CENTER);
		title_bar = inflater.inflate(R.layout.add_experience_action_bar, null);
		
		actionBar.setCustomView(title_bar,lp);
		actionBar.setDisplayShowHomeEnabled(false);
		actionBar.setDisplayShowTitleEnabled(false);
		actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
		actionBar.setDisplayShowCustomEnabled(true);
	}
	private void initComponent(){
		save_tv = (TextView) title_bar.findViewById(R.id.save);
		begin_time_rl = (RelativeLayout) findViewById(R.id.begin_time_rl);
		end_time_rl = (RelativeLayout) findViewById(R.id.end_time_rl);
		begin_time_tv = (TextView) findViewById(R.id.begin_time_tv);
		end_time_tv = (TextView) findViewById(R.id.end_time_tv);
		title_tv = (TextView) findViewById(R.id.title_tv);
		hospital_tv = (TextView) findViewById(R.id.hospital_tv);
		department_tv = (TextView) findViewById(R.id.department_tv);
		title_rl = (RelativeLayout) findViewById(R.id.title_rl);
		hospital_rl = (RelativeLayout) findViewById(R.id.hospital_rl);
		department_rl = (RelativeLayout) findViewById(R.id.department_rl);
		
	}
	
	private void initEventListener(){
		save_tv.setOnClickListener(this);
		begin_time_rl.setOnClickListener(this);
		end_time_rl.setOnClickListener(this);
		title_rl.setOnClickListener(this);
		hospital_rl.setOnClickListener(this);
		department_rl.setOnClickListener(this);
		
	}

	@SuppressWarnings("deprecation")
	@SuppressLint("NewApi")
	@Override
	public void onClick(View v) {
		Intent intent;
		switch(v.getId()){
		case R.id.begin_time_rl:
			 showDialog(DATE_DIALOG_ID); 
			break;
		case R.id.end_time_rl:
			 showDialog(DATE_DIALOG_ID_END); 
			break;
		case R.id.title_rl:
			intent = new Intent(AddExperienceActivity.this, GetTitleActivity.class);
			startActivityForResult(intent, 1);
			break;
		case R.id.save:
			addExp();
			break;
		case R.id.hospital_rl:
			intent = new Intent(AddExperienceActivity.this, AddHospitalActivity.class);
			startActivityForResult(intent, 2);
			break;
		case R.id.department_rl:
			intent = new Intent(AddExperienceActivity.this, AddDepartmentActivity.class);
			startActivityForResult(intent, 3);
			break;
		default:
				break;
		}
		
	}
	
	/**
	 * 
	 */
	private void addExp() {
		Toast.makeText(context, "添加从医经历", Toast.LENGTH_SHORT).show();
		RequestParams params = new RequestParams();
		params.add("fromYear", bYear + "");
	    params.add("fromMonth", bMonth + "");
	    params.add("endYear", eYear + "");
	    params.add("endMonth", eMonth + "");
	    params.add("hospital", hospital);
	    params.add("section", department);
	    params.add("title", title);
		YJRHttpClient.post(context,"/doctor/addExperience", params, new JsonHttpResponseHandler(){
			 @Override  
	            public void onSuccess(JSONObject data) {
	             	jsonInfo = data;
	             	try {
						if(jsonInfo.getInt("result") == 1){
							YJRHttpClient.setCookie(context); 
							Toast.makeText(context, "添加从医经历成功", Toast.LENGTH_LONG).show();
							Intent intent = new Intent();
							((Activity) context).setResult(Activity.RESULT_OK,intent);
							AddExperienceActivity.this.finish();
						}else{
							Toast.makeText(context, jsonInfo.getString("info"), Toast.LENGTH_SHORT).show();
						}
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
	            }  
		});
		
	}

	/** 
     * 当Activity调用showDialog函数时会触发该函数的调用： 
     */  
    @Override  
    protected Dialog onCreateDialog(int id) {  
           switch (id) {  
            case DATE_DIALOG_ID:  
                return new DatePickerDialog(this,mDateSetListener,2000, 0, 1); 
            case DATE_DIALOG_ID_END: 
            	return new DatePickerDialog(this,mEndDateSetListener,2014, 4, 1); 
            }  
            return null;  
    }  
    
    
  //需要定义弹出的DatePicker对话框的事件监听器：  
    private DatePickerDialog.OnDateSetListener mDateSetListener =new OnDateSetListener() {  
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {  
                bYear = year;  
                bMonth = monthOfYear + 1;    
                //设置文本的内容：  
                begin_time_tv.setText(bYear + "年" + bMonth + "月");
            }
        };  
        private DatePickerDialog.OnDateSetListener mEndDateSetListener =new OnDateSetListener() {  
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {  
                eYear = year;  
                eMonth = monthOfYear + 1;    
                //设置文本的内容：  
                end_time_tv.setText(eYear + "年" + eMonth + "月");
            }
        };  
        
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
  	      switch (requestCode) {
  	      case 1:
  	    	  title = data.getStringExtra("title");
  	    	  title_name = data.getStringExtra("title_name");
  	    	  if(title_name!=null){
  	    		  title_tv.setText(title_name);
  	    	  }
  	    	  //Toast.makeText(context, title, Toast.LENGTH_SHORT).show();
  	    	  break;
  	      case 2:
  	    	  hospital = data.getStringExtra("name");
  	    	  if(hospital!=null){
  	    		  hospital_tv.setText(hospital);
  	    	  }
  	    	  break;
  	      case 3:
  	    	  department = data.getStringExtra("name");
  	    	  if(department!=null){
  	    		  department_tv.setText(department);
  	    	  }
  	    	  break;
  	      default:  
  	          break;  

  	      }  
  	      super.onActivityResult(requestCode, resultCode, data);  
  	  }  

}